<template>
  <div class="postlist">
    <!-- 路径导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item
        ><a href="javascript:;">文章管理</a></el-breadcrumb-item
      >
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="box-card" style="margin-top: 20px">
      <!-- 表格 -->
      <el-table :data="postlist" border style="width: 100%">
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column prop="title" label="标题" width="680">
        </el-table-column>
        <el-table-column label="类型" width="100">
          <template slot-scope="scope">
            {{ scope.row.type == 1 ? "文章" : "视频" }}
          </template>
        </el-table-column>
        <el-table-column prop="user.nickname" label="作者"> </el-table-column>
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <!-- scope就是当前的数据行对象，包含当前行数据，索引....
            scope.row就是当前的行数据，我们以后进行数据操作的时候，主要是对这个值进行处理 -->
            <el-tooltip
              class="item"
              effect="dark"
              content="编辑"
              placement="top"
            >
              <el-button type="primary" icon="el-icon-edit"></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="分享"
              placement="top"
            >
              <el-button type="success" icon="el-icon-share"></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="删除"
              placement="top"
            >
              <el-button type="warning" icon="el-icon-delete"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 20px"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getPostList } from "@/apis/post.js";
export default {
  data() {
    return {
      postlist: [],
      total: 0, // 查询到的数据的总数，注意不是当前分页操作的总数
      pageSize: 2, // 每页显示的数量
      pageIndex: 1, // 当前页码
    };
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      // val：就是当前用户所需要展示的数量
      // 重置pageSize
      this.pageSize = val;
      // 让axios根据重置后的pageSize发请求获取数据
      this.init();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      // val：就是当前用户所单击的页码
      this.pageIndex = val;
      this.init();
    },
    async init() {
      let res = await getPostList({
        pageSize: this.pageSize,
        pageIndex: this.pageIndex,
      });
      console.log(res);
      this.postlist = res.data.data;
      this.total = res.data.total;
    },
  },
  async mounted() {
    // 获取文章列表数据
    this.init();
  },
};
</script>

<style>
</style>